'use strict'
//不要问为什么有一个tools.js了还要分这么多工具文件不存在一个js工具文件，问就是懒
export default {
    //清除html标签
    clearRichText(texts){
        if(texts){
            let text1 = texts.replace(/<\/?(img|table)[^>]*>/g,"")
            let text2 = text1.replace(/<\/?.+?>/g,"")
            let text3 = text2.replace(/[ | ]*\n/g,"")
            let text4 = text3.replace(/ /g,"")
            return text4
        }
    },
    formatTimeString(timeStr){
        const regex = /(\d+)小时(\d+)分/;
        const match = timeStr.match(regex);
        if (match) {
            const hours = parseInt(match[1], 10);
            const minutes = parseInt(match[2], 10);
            const seconds = hours * 3600 + minutes * 60;
            const now = new Date();
            const timestamp = now.getTime() + seconds * 1000; // JavaScript时间戳是以毫秒为单位的
            return timestamp;
        }
        return null; // 如果字符串不匹配则返回null
    },
    consoleStylelog(text,date){
        console.log(` %c ${text} %c ${date}`,
            'background: #35495e; padding: 4px; border-radius: 3px 0 0 3px; color: #fff',
            'background: #41b883; padding: 4px; border-radius: 0 3px 3px 0; color: #fff',
        );
    },
    //控制台打印图片
    consoleImage(url, scale) {
        const img = new Image()
        img.crossOrigin = "anonymous"
        img.onload = () => {
            const c = document.createElement('canvas')
            const ctx = c.getContext('2d')
            if (ctx) {
                c.width = img.width
                c.height = img.height
                ctx.fillRect(0, 0, c.width, c.height);
                ctx.drawImage(img, 0, 0)
                const dataUri = c.toDataURL('image/png')

                console.log(`%c sup?` ,
                    `
          font-size: 1px;
          padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
          background-image: url(${dataUri});
          background-repeat: no-repeat;
          background-size: ${img.width * scale}px ${img.height * scale}px;
          color: transparent;
        `
                )
            }
        }
        img.src = url
    },
    //指定星期打印
    printOnSpecificDay(dayOfWeek, text,time,imgUrl,scale) {
        // 验证dayOfWeek是否有效（0-6分别代表周日至周六）
        if (dayOfWeek < 0 || dayOfWeek > 6) {
            throw new Error('Invalid day of week. Must be between 0 (Sunday) and 6 (Saturday).');
        }

        // 获取今天的星期几
        const today = new Date();
        const currentDayOfWeek = today.getDay();

        // 计算距离目标星期还有多少天
        let daysUntilTargetDay = (dayOfWeek - currentDayOfWeek + 7) % 7;

        // 定义打印函数
        let _this = this
        function printText() {
            const now = new Date();
            if (now.getDay() === dayOfWeek) {
                _this.consoleStylelog(text,time)
                _this.consoleImage(imgUrl,scale)
            }
        }

        // 设置定时器，在指定的时间后首次执行printText函数，之后每天执行一次
        const intervalId = setInterval(printText, daysUntilTargetDay * 24 * 60 * 60 * 1000);
        setTimeout(() => {
            // 调整首次执行的时间差，使其立即执行一次
            clearInterval(intervalId);
            printText();
            // 重新设置定时器，每天执行一次
            setInterval(printText, 24 * 60 * 60 * 1000);
        }, daysUntilTargetDay * 24 * 60 * 60 * 1000);
    }

}
